﻿@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Gurux.DLMS.AMI.Module
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Gurux.DLMS.AMI.Shared.Enums
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Microsoft.AspNetCore.SignalR.Client

@namespace Gurux.DLMS.AMI.Client.Pages.Config

@attribute [Authorize(Roles = GXRoles.Admin)]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier2 Notifier
@implements IDisposable

@if (_languages != null)
{
    <h1>Available languages</h1>
    <table class="table table-striped" width="100%">
        <thead>
            <tr>
                <th>@Properties.Resources.Active</th>
                <th>@Properties.Resources.Name</th>
                <th>默认语言</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var it in _languages)
            {
                <tr class=@(GetSelectedClass(it)) @onclick="@(()=>RowSelected(it))">
                    <td>
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox"
                                   checked="@it.Active"
                           @onchange="@((e) => it.Active = Convert.ToBoolean(e.Value))" />
                        </div>
                    </td>
                    <td>
                        @it.EnglishName
                    </td>
                    <td>
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox"
                                   disabled="@IsActive(it)" checked="@it.Default"
                           @onchange="@((e) => it.Default = Convert.ToBoolean(e.Value))" />
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}
@code {
    GXLanguage[]? _languages = null;
    private GXLanguage? _active = null;
    //If language is not active it can't be the default language.
    protected string? IsActive(GXLanguage selected)
    {
        return selected.Active.HasValue && selected.Active.Value ? null : "disabled";
    }

    protected string GetSelectedClass(GXLanguage selected)
    {
        return _active != null && selected.Id == _active.Id ? "table-info" : "table-striped";
    }

    protected void RowSelected(GXLanguage selected)
    {
        _active = selected;
        Notifier.UpdateData(GXPages.Modules, _active);
    }


    /// <summary>
    /// Update virtualized items.
    /// </summary>
    protected async Task Updated()
    {
        try
        {
            _languages = null;
            Notifier?.ProgressStart();
            Notifier?.ClearStatus();
            _languages = (await Http.PostAsJson<ListLanguagesResponse>("api/Localization/List", new ListLanguages())).Languages;
            StateHasChanged();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    protected override async Task OnInitializedAsync()
    {
        try
        {
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            Notifier.On<IEnumerable<IEnumerable<GXLanguage>>>(this, nameof(IGXHubEvents.LanguageUpdate), async (languages) =>
            {
                await Updated();
            });
            Notifier.Clear();
            Notifier.AddMenuItem(new GXMenuItem() { Text = Properties.Resources.Refresh, Icon = "oi oi-reload", OnClick = OnRefresh });
            Notifier.AddMenuItem(new GXMenuItem() { Text = Properties.Resources.Save, Icon = "oi oi-pencil", OnClick = OnSave });
            Notifier.AddMenuItem(new GXMenuItem() { Text = Properties.Resources.Cancel, Icon = "oi oi-action-undo", OnClick = OnCancel });
            Notifier.UpdateButtons();
            //Read languages as a default.
            await Updated();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Save values.
    /// </summary>
    private async void OnSave()
    {
        try
        {
            Notifier?.ProgressStart();
            Notifier?.ClearStatus();
            List<GXLanguage> list = new List<GXLanguage>();
            //Only ID, Default and active states are sent.
            bool defaultLanguage = false;
            foreach (var it in _languages)
            {
                list.Add(new GXLanguage() { Id = it.Id, Active = it.Active, Default = it.Default });
                if (it.Active.HasValue && it.Active.Value &&
                    it.Default.HasValue && it.Default.Value)
                {
                    defaultLanguage = true;
                }
            }
            if (!defaultLanguage)
            {
                throw new ArgumentException("Default language is not selected.");
            }
            await Http.PostAsJson("api/Localization/Update", _languages);
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Refresh values.
    /// </summary>
    private async void OnRefresh()
    {
        try
        {
            Notifier?.ProgressStart();
            Notifier?.ClearStatus();
            List<GXLanguage> list = new List<GXLanguage>();
            //Only active language IDs are send.
            foreach (var it in _languages)
            {
                if (it.Active.HasValue && it.Active.Value)
                {
                    list.Add(new GXLanguage() { Id = it.Id });
                }
            }
            RefreshLocalization req = new RefreshLocalization();
            await Http.PostAsJson("api/Localization/Refresh", req);
            Notifier?.ShowInformation(Properties.Resources.Refreshed);
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Cancel update.
    /// </summary>
    private void OnCancel()
    {
        ClientHelpers.NavigateToLastPage(NavigationManager, Notifier);
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}
